import urllib.request
from urllib import request,error
import json
import time,datetime
from time import sleep

def senderror(errcont):
    enow=datetime.datetime.now()
    now=enow.strftime('%Y-%m-%d %H:%M:%S')
    errfile=open('DDNS.log','a')
    errfile.write(now)
    errfile.write(str(errcont))
    errfile.write('\n')
    errfile.close()



def getIpPage():
    # print(11111111111111111111111111111111111)
    try:
        url = "https://api.ipify.org/?format=json"
        response = request.urlopen(url,timeout=60)
        html = response.read().decode('utf-8')
        # print(11111111111111111,html)
        return html
    except error.HTTPError as e:
        print(e.reason)
        senderror(e.reason)
        time.sleep(10)
        getIpPage()
    except error.URLError as e:
        print(e.reason)
        senderror(e.reason)
        time.sleep(10)
        getIpPage()
    except:
        url = "http://members.3322.org/dyndns/getip"
        response = request.urlopen(url,timeout=60)
        html = response.read().decode('utf-8')
        html = html.replace("\n","")
        htm={}
        htm['ip'] = html
        HTL = json.dumps(htm)
        return HTL

# 解析数据，获得IP
def getRealIp(data):
    try:
        jsonData = json.loads(data)
        # print(11111111,jsonData)
        return jsonData['ip']
    except:
        time.sleep(200)
        # print(2222222222, jsonData)
        getIpPage()


# 利用API获取含有用户ip的JSON数据
def getIpPageV6():
    url = "https://v6.ident.me/.json"
    response = urllib.request.urlopen(url)
    html = response.read().decode('utf-8')
    return html


# 解析数据，获得IP
def getRealIpV6(data):
    jsonData = json.loads(data)
    return jsonData['address']
